<?$preferences = getPreferences($loggedInTeam->id);?>
<script>
function moveUp(frm) {
	if (frm.selectedPlayers.selectedIndex == 0 || frm.selectedPlayers.selectedIndex == -1) return;
	for (i = 0; i < frm.selectedPlayers.options.length; i++) {
		if (i == frm.selectedPlayers.selectedIndex) {
			oldValue = frm.selectedPlayers.options[i-1].value;
			oldText = frm.selectedPlayers.options[i-1].text;
			frm.selectedPlayers.options[i-1].value = frm.selectedPlayers.options[i].value
			frm.selectedPlayers.options[i-1].text = frm.selectedPlayers.options[i].text
			frm.selectedPlayers.options[i].value = oldValue
			frm.selectedPlayers.options[i].text = oldText
			frm.selectedPlayers.selectedIndex = i - 1;
			
			return;
		}
	}
}
function moveDown(frm) {
	if (frm.selectedPlayers.selectedIndex == frm.selectedPlayers.options.length - 1 || frm.selectedPlayers.selectedIndex == -1) return;
	for (i=0; i<frm.selectedPlayers.options.length; i++) {
		if (i == frm.selectedPlayers.selectedIndex) {
			oldValue = frm.selectedPlayers.options[i+1].value;
			oldText = frm.selectedPlayers.options[i+1].text;
			frm.selectedPlayers.options[i+1].value = frm.selectedPlayers.options[i].value
			frm.selectedPlayers.options[i+1].text = frm.selectedPlayers.options[i].text
			frm.selectedPlayers.options[i].value = oldValue
			frm.selectedPlayers.options[i].text = oldText
			frm.selectedPlayers.selectedIndex = i + 1;
			
			return;
		}
	}
}
var prefFormSubmitted = false;
function submitFormForPref(frm) {
	if (!prefFormSubmitted) {
		for (i=0; i<frm.selectedPlayers.options.length;i++) {
			frm.selectedPlayers.options[i].selected = true;
		}
		
		prefFormSubmitted = true;
		frm.submit();
	}
}
function removeFromList(frm) {
	frm.selectedPlayers.options[frm.selectedPlayers.selectedIndex] = null;
}
function removePreferences(frm) {
	for (count = 0; count < frm.selectedPlayers.options.length; count++) {
		for (count2 = 0; count2 < frm.availPlayers.options.length; count2++) {
			if (frm.availPlayers.options[count2].value == frm.selectedPlayers.options[count].value) {
				frm.availPlayers.options[count2] = null;
				break;
			}
		}
	}
}
</script>
<form id="reqFormForPref" action="../makePickAndPref/submitMakeMyPreference.php">
<input type="hidden" path="teamId" />
<table>
	<tr>
		<td><b>Search and select player in left and move to the right once all preferences selected click Submit at the bottom. If you want these prereferences to be used remember to change your preferences settings in the Settings tab.</b><br><br></td>
	</tr>
	<tr>
		<td><input type="text" onkeyup="filterSelect(this.value, this.form, false)"></input>&nbsp;Start typing name to filter list</td>
	</tr>
	<tr>
		<td>
		<table>
		<tr>
		<td>
			<select name="availPlayers" size="20" style="width: 270px;">
			</select> 
		</td>
		<td align="top"><input type="button" value="Up" onclick="moveUp(this.form)" class="buttonStyle"><br><br><input type="button" value="Down" onclick="moveDown(this.form)" class="buttonStyle"><br><br><input type="button" value="Add" onclick="addNewPlayerToSelect(this.form, 'selectedPlayers')" class="buttonStyle"><br><br><input type="button" value="Remove" onclick="removeFromList(this.form)" class="buttonStyle"></td>
		<td>
		<select id="selectedPlayers" name="selectedPlayers[]" size=20 style="width: 270px;" multiple>
		<?
		$count = count($preferences);
		for ($i = 0; $i < $count; $i++) {?>
			<option value="<?echo $preferences[$i]->id?>"><?echo $preferences[$i]->getDescription()?></option>
		<?}?>
		</select>
		</td>
		</tr>
		<tr>
		<td colspan="3"><input type="button" value="Submit" onclick="submitFormForPref(this.form)" class="buttonStyle" /></td>
		</tr>		
		</table>
		</td>
	</tr>
</table>
</form>

<script>populateSelect(document.getElementById("reqFormForPref"), false);removePreferences(document.getElementById("reqFormForPref"));</script>